<template>
  <!-- <transition name="fade"> -->
    <!-- 筛选区 -->
    <div class="sub-filter" v-if="list.length">
      <div class="item" v-for="item in list" :key="item.id">
        <div class="head">{{ item.name }}</div>
        <div class="body">
          <!-- 默认情况下是全部选项高亮，那么把全部的值设置成 undefined -->
          <a
            href="javascript:;"
            @click="getAttr(item,undefined)"
            :class="{ active: item.activeId === undefined }"
            >全部</a
          >
          <!-- 点击高亮效果 -->
          <a
            href="javascript:;"
            @click="getAttr(item, propertie.id)"
            :class="{ active: item.activeId === propertie.id }"
            v-for="propertie in item.properties"
            :key="propertie.id"
            >{{ propertie.name }}</a
          >
        </div>
      </div>
    </div>
    <div v-else class="sub-filter">
      <XtxSkeleton class="item" width="800px" height="40px" />
      <XtxSkeleton class="item" width="800px" height="40px" />
      <XtxSkeleton class="item" width="600px" height="40px" />
      <XtxSkeleton class="item" width="600px" height="40px" />
      <XtxSkeleton class="item" width="600px" height="40px" />
    </div>
  <!-- </transition> -->
</template>
<script>
import { findSubCategoryFilter } from '@/api/category'
import { useRoute } from 'vue-router'
import { ref, watch } from 'vue'
export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    const list = ref([])
    //   attrs: [ { groupName: '属性组名称', propertyName: '属性(值)名称' } ]
    //   attrs: [ { groupName: '颜色', propertyName: 'val' }, { groupName: '规格', propertyName: 'val' }  ]
    let attrs = []

    const route = useRoute()
    const getFilter = async (id) => {
      const { result } = await findSubCategoryFilter(id)
      console.log(result)
      list.value = []
      // result.brands            品牌数据
      // result.saleProperties    筛选条件
      // console.log(result)
      // 把品牌和筛选条件放到了同一个数组内
      list.value.push({ id: null, name: '品牌', properties: result.brands })
      list.value.push(...result.saleProperties)
      console.log(list.value)
    }
    watch(
      () => route.params.id,
      (id) => {
        if (route.path === `/category/sub/${id}`) {
          // 重置属性
          attrs = []
          // 重置品牌
          brandId = undefined
          getFilter(id)
        }
      },
      { immediate: true }
    )

    let brandId
    const getAttr = (theItem, id) => {
      console.log(theItem, id)
      // 实现高亮效果
      theItem.activeId = id

      // 情况1：品牌
      if (theItem.name === '品牌') {
        brandId = id
      } else {
        // 情况2：属性 查找当前字段是否已存在
        const index = attrs.findIndex((item) => item.groupName === theItem.name)
        if (index === -1) {
          // 不存在则添加对象
          attrs.push({ groupName: theItem.name, propertyName: id })
        } else {
          // 如果存在，修改值即可
          attrs[index].propertyName = id
        }
      }

      const filterData = {}
      // 如果有品牌 id 就添加到请求参数中
      if (brandId !== undefined) {
        filterData.brandId = brandId
      }
      // 过滤掉无效的值
      filterData.attrs = attrs.filter(item => item.propertyName !== undefined)
      console.log(filterData)
      emit('filter-change', filterData)
    }

    return { list, getAttr }
  }
}
</script>
<style scoped lang='less'>
.xtx-skeleton {
  padding: 10px 0;
}
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
